﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

using System.Data.SqlClient;
using System.Data.Common;
/// <summary>
/// Summary description for CMIDB
/// </summary>
/// 
namespace CE.CMI
{
    public class CMIDB
    {
        public CMIDB()
        {
        }

        public Int64 InsertUpdate(CMI Cmionline)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCMIOnlineInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@CMIId", Cmionline.CMIId);
            _sPara.Add("@RefCEUserId", Cmionline.RefCEUserId);
            _sPara.Add("@QuoteRefNo", Cmionline.QuoteRefNo); //No Update
            _sPara.Add("@CECarId", Cmionline.CECarId); //No Update

            _sPara.Add("@ServiceType", Cmionline.ServiceType);
            _sPara.Add("@CarType", Cmionline.CarType); //No Update
            _sPara.Add("@CTPLProviderCompanyId", Cmionline.CTPLProviderCompanyId);
            if (Cmionline.CMIDate != null)
            {
                _sPara.Add("@CMIDate", Cmionline.CMIDate.Value);
            }
            else
            {
                _sPara.Add("@CMIDate", DBNull.Value);
            }
            if (Cmionline.RegistrationDate != null)
            {
                _sPara.Add("@RegistrationDate", Cmionline.RegistrationDate.Value);
            }
            else
            {
                _sPara.Add("@RegistrationDate", DBNull.Value);
            }
            _sPara.Add("@CarWeight", Cmionline.CarWeight);
            _sPara.Add("@TaxRenewalAmount", Cmionline.TaxRenewalAmount);
            _sPara.Add("@CTPLAmout", Cmionline.CTPLAmout);
            _sPara.Add("@CTPLDiscount", Cmionline.CTPLDiscount);
            _sPara.Add("@ChargedAmount", Cmionline.ChargedAmount);
            _sPara.Add("@EMS", Cmionline.EMS);
            _sPara.Add("@CMITotal", Cmionline.CMITotal);
            _sPara.Add("@CarMake", Cmionline.CarMake);
            _sPara.Add("@CarModel", Cmionline.CarModel);

            _sPara.Add("@Body", Cmionline.Body);
            _sPara.Add("@CC", Cmionline.CC);
            _sPara.Add("@Door", Cmionline.Door);
            _sPara.Add("@ManufactYear", Cmionline.ManufactYear);
            _sPara.Add("@MakeModelDesc", Cmionline.MakeModelDesc);
            _sPara.Add("@ChassisNo", Cmionline.ChassisNo);
            _sPara.Add("@EngineNumber", Cmionline.EngineNumber);
            _sPara.Add("@EngineSize", Cmionline.EngineSize);
            _sPara.Add("@CarColor", Cmionline.CarColor);
            _sPara.Add("@Name", Cmionline.Name);
            _sPara.Add("@OwnerName", Cmionline.OwnerName);
            _sPara.Add("@CarPlate", Cmionline.CarPlate);
            _sPara.Add("@Province", Cmionline.Province);
            _sPara.Add("@Address", Cmionline.Address);
            _sPara.Add("@Email", Cmionline.Email);
            _sPara.Add("@Mobile", Cmionline.Mobile);
            _sPara.Add("@DeliveryName", Cmionline.DeliveryName);
            _sPara.Add("@DeliveryAddress", Cmionline.DeliveryAddress);
            _sPara.Add("@ProviderCompanyId", Cmionline.ProviderCompanyId);
            _sPara.Add("@ExpiryMonth", Cmionline.ExpiryMonth);
            _sPara.Add("@IsEnglish", Cmionline.IsEnglish);
            _sPara.Add("@Status", Cmionline.Status);
            _sPara.Add("@IsSameAddress", Cmionline.IsSameAddress);
            _sPara.Add("@TrackingCode", Cmionline.TrackingCode);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public Int64 UpdateStatus(Int64 CMIId, string OrderRefNo, string QRefNo, string PaymentType)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocCMIOnlineUpdateStatus]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@CMIId", CMIId);
            _sPara.Add("@OrderRefNo", OrderRefNo);
            _sPara.Add("@QuoteRefNo", QRefNo);
            _sPara.Add("@PaymentType", PaymentType);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CMI GetCMI(Int64 CMIId)
        {
            CMI _cmi = null;
            MyDatabase _db = new MyDatabase();

            using (DataTable _dt = _db.GetDataTable("SELECT * FROM tblCMIDetail WHERE CMIId=" + CMIId.ToString()))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _cmi = new CMI();
                    _cmi = FillDataRecord(_dt, 0);
                }
            }
            return _cmi;
        }

        private CMI FillDataRecord(DataTable myDataTable, int RowId)
        {
            CMI myCMI = new CMI();

            myCMI.CMIId = Convert.ToInt64(myDataTable.Rows[RowId]["CMIId"].ToString());
            myCMI.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCMI.QuoteRefNo = myDataTable.Rows[RowId]["QuoteRefNo"].ToString();
            myCMI.OrderRefNo = myDataTable.Rows[RowId]["OrderRefNo"]!= DBNull.Value ? myDataTable.Rows[RowId]["OrderRefNo"].ToString():"";
            if (myDataTable.Rows[RowId]["CECarId"].ToString() != "")
                myCMI.CECarId = Convert.ToInt64(myDataTable.Rows[RowId]["CECarId"].ToString());
            myCMI.ServiceType = Convert.ToChar(myDataTable.Rows[RowId]["ServiceType"]);
            myCMI.CarType = Convert.ToChar(myDataTable.Rows[RowId]["CarType"]);
            myCMI.CTPLProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["CTPLProviderCompanyId"].ToString());
            if (myDataTable.Rows[RowId]["CMIDate"].ToString() != "")
            {
                myCMI.CMIDate = Convert.ToDateTime(myDataTable.Rows[RowId]["CMIDate"].ToString());
            }
            if (myDataTable.Rows[RowId]["RegistrationDate"].ToString() != "")
            {
                myCMI.RegistrationDate = Convert.ToDateTime(myDataTable.Rows[RowId]["RegistrationDate"].ToString());
            }
            myCMI.TaxRenewalAmount = Convert.ToDecimal(myDataTable.Rows[RowId]["TaxRenewalAmount"].ToString());
            myCMI.CTPLAmout = Convert.ToDecimal(myDataTable.Rows[RowId]["CTPLAmout"].ToString());
            myCMI.CTPLDiscount = Convert.ToDecimal(myDataTable.Rows[RowId]["CTPLDiscount"].ToString());
            myCMI.ChargedAmount = Convert.ToDecimal(myDataTable.Rows[RowId]["ChargedAmount"].ToString());
            myCMI.EMS = Convert.ToDecimal(myDataTable.Rows[RowId]["EMS"].ToString());
            myCMI.CMITotal = Convert.ToDecimal(myDataTable.Rows[RowId]["CMITotal"].ToString());
            myCMI.CarMake = myDataTable.Rows[RowId]["CarMake"].ToString();
            myCMI.CarModel = myDataTable.Rows[RowId]["CarModel"].ToString();
            myCMI.CarWeight = myDataTable.Rows[RowId]["CarWeight"].ToString();

            myCMI.Body = myDataTable.Rows[RowId]["Body"].ToString();
            myCMI.CC = Convert.ToInt32(myDataTable.Rows[RowId]["CC"].ToString());
            myCMI.Door = Convert.ToInt32(myDataTable.Rows[RowId]["Door"].ToString());
            myCMI.ManufactYear = Convert.ToInt32(myDataTable.Rows[RowId]["ManufactYear"].ToString());
            myCMI.MakeModelDesc = myDataTable.Rows[RowId]["MakeModelDesc"].ToString();
            myCMI.ChassisNo = myDataTable.Rows[RowId]["ChassisNo"].ToString();
            myCMI.EngineNumber = myDataTable.Rows[RowId]["EngineNumber"].ToString();
            myCMI.EngineSize = myDataTable.Rows[RowId]["EngineSize"].ToString();
            myCMI.CarColor = myDataTable.Rows[RowId]["CarColor"].ToString();
            myCMI.Name = myDataTable.Rows[RowId]["Name"].ToString();
            myCMI.OwnerName = myDataTable.Rows[RowId]["OwnerName"].ToString();
            myCMI.CarPlate = myDataTable.Rows[RowId]["CarPlate"].ToString();
            myCMI.Province = myDataTable.Rows[RowId]["Province"].ToString();
            myCMI.Address = myDataTable.Rows[RowId]["Address"].ToString();
            myCMI.Email = myDataTable.Rows[RowId]["Email"].ToString();
            myCMI.Mobile = myDataTable.Rows[RowId]["Mobile"].ToString();
            myCMI.DeliveryName = myDataTable.Rows[RowId]["DeliveryName"].ToString();
            myCMI.DeliveryAddress = myDataTable.Rows[RowId]["DeliveryAddress"].ToString();
            myCMI.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCMI.ExpiryMonth = myDataTable.Rows[RowId]["ExpiryMonth"].ToString();
            if (myDataTable.Rows[RowId]["IsEnglish"].ToString() == "0")
            {
                myCMI.IsEnglish = false;
            }
            else
            {
                myCMI.IsEnglish = true;
            }
            myCMI.Status = myDataTable.Rows[RowId]["Status"].ToString();

            myCMI.IsSameAddress = Convert.ToBoolean(myDataTable.Rows[RowId]["IsSameAddress"].ToString());
            myCMI.TrackingCode = myDataTable.Rows[RowId]["TrackingCode"].ToString();

            if (myDataTable.Rows[RowId]["DeliveryDate"].ToString() != "")
            {
                myCMI.DeliveryDate = Convert.ToDateTime(myDataTable.Rows[RowId]["DeliveryDate"].ToString());
            }

            return myCMI;
        }


    }
}
